{extend name="adminblue/base" /}
{block name="resources"/}
<style>
.table-class input{color:#666;width:206px;margin-bottom: 0px;border: 1px solid #ccc;}
.table-class input.sort{width: auto;text-align: center;}
</style>
{/block}
{block name="right_tab"}
<li><a href="{:__URL('ADMIN_MAIN/goods/addgoodscategory')}"><i class="fa fa-plus-circle"></i>&nbsp;添加商品分类</a></li>
{/block}
{block name="main"}
{notempty name="$category_list"}
<table class="table-class">
	<colgroup>
		<col style="width: 1%;">
		<col style="width: 5%;">
		<col style="width: 29%;">
		<col style="width: 29%;">
		<col style="width: 16%;">
		<col style="width: 10%;">
		<col style="width: 10%;">
	</colgroup>
	<thead>
		<tr align="center">
			<th></th>
			<th>排序</th>
			<th style="text-align: left;">商品分类</th>
			<th style="text-align: left;">商品分类简称</th>
			<th style="text-align: left;">关联类型</th>
			<th>是否显示</th>
			<th>操作</th>
		</tr>
	</thead>
	<colgroup>
		<col style="width: 1%;">
		<col style="width: 5%;">
		<col style="width: 29%;">
		<col style="width: 29%;">
		<col style="width: 16%;">
		<col style="width: 10%;">
		<col style="width: 10%;">
	</colgroup>
	<tbody>
		{foreach name="category_list" item="v1"}
			<tr class="pid_0" style="height: 30px;">
				<td>
					{if condition="$v1['child_list'] neq array()"}
					<a href="javascript:;" onclick="tab_switch({$v1['category_id']})" class="tab_jia_{$v1['category_id']}" style="display: block;"><i class="fa fa-plus-circle"></i></a>
					<a href="javascript:;" onclick="tab_switch({$v1['category_id']})" class="tab_jian_{$v1['category_id']}" style="display: none;"><i class="fa fa-minus-circle"></i></a>
					{/if}
				</td>
				<td style="text-align: center;"><input type="text" class="sort input-common" fieldid="{$v1['category_id']}" fieldname="sort" value="{$v1['sort']}" size="1"></td>
				<td><input class="input-common" type="text" fieldid="{$v1['category_id']}" fieldname="category_name" value="{$v1['category_name']}" style="width: 150px;"></td>
				<td><input class="input-common" type="text" fieldid="{$v1['category_id']}" fieldname="short_name" value="{$v1['short_name']}" style="width: 150px;"></td>
				<td>{$v1['attr_name']}</td>
				<td style="text-align: center;">{$v1['is_visible'] ? '是' : '否'}</td>
				<td style="text-align: center;">
					<a href="{:__URL('ADMIN_MAIN/goods/updategoodscategory','category_id='.$v1['category_id'])}">修改</a>
					<a href="javascript:void(0);" onclick="delCategory({$v1['category_id']})">删除</a>
				</td>
			</tr>
			{foreach name="v1['child_list']" item="v2"}
			<tr class="pid_{$v1['category_id']}" style="height: 30px;display: none;">
				<td>
					{if condition="$v2['child_list'] neq array()"}
					<a href="javascript:;" onclick="tab_switch({$v2['category_id']})" class="tab_jian_{$v2['category_id']}" style="display: block;"><i class="fa fa-minus-circle"></i></a>
					<a href="javascript:;" onclick="tab_switch({$v2['category_id']})" class="tab_jia_{$v2['category_id']}" style="display: none;"><i class="fa fa-plus-circle"></i></a>
					{/if}
				</td>
				<td style="text-align: center;"><input type="text" class="sort input-common" fieldid="{$v2['category_id']}" fieldname="sort"  value="{$v2['sort']}" size="1"></td>
				<td><span style="color:#ccc;">|——</span> <input type="text" class="input-common" fieldid="{$v2['category_id']}" fieldname="category_name" value="{$v2['category_name']}" style="width: 100px;"></td>
				<td><input type="text" class="input-common" fieldid="{$v2['category_id']}" fieldname="short_name" value="{$v2['short_name']}" style="width: 150px;"></td>
				<td>{$v2['attr_name']}</td>
				<td style="text-align: center;">{$v2['is_visible'] ? '是' : '否'}</td>
				<td style="text-align: center;">
					<a href="{:__URL('ADMIN_MAIN/goods/updategoodscategory','category_id='.$v2['category_id'])}">修改</a>
					<a href="javascript:void(0);" onclick="delCategory({$v2['category_id']})">删除</a>
				</td>
			</tr>
				{foreach name="v2['child_list']" item="v3"}
				<tr class="pid_{$v2['category_id']} pid_{$v1['category_id']}" style="height: 30px;display:none;">
					<td></td>
					<td style="text-align: center;"><input type="text" class="sort input-common" fieldid="{$v3['category_id']}" fieldname="sort"  value="{$v3['sort']}" size="1"></td>
					<td><span style="color:#ccc;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|——</span> <input class="input-common type="text" fieldid="{$v3['category_id']}" fieldname="category_name" value="{$v3['category_name']}" style="width: 100px;"></td>
					<td><input type="text" class="input-common" fieldid="{$v3['category_id']}" fieldname="short_name" value="{$v3['short_name']}" style="width: 150px;"></td>
					<td>{$v3['attr_name']}</td>
					<td style="text-align: center;">{$v3['is_visible'] ? '是' : '否'}</td>
					<td style="text-align: center;">
						<a href="{:__URL('ADMIN_MAIN/goods/updategoodscategory','category_id='.$v3['category_id'])}">修改</a>
						<a href="javascript:void(0);" onclick="delCategory({$v3['category_id']})">删除</a>
					</td>
				</tr>
				{/foreach}
			{/foreach}
		{/foreach}
	</tbody>
</table>
{else/}
	<div class="tip-box">当前系统还没有商品分类，<a href="{:__URL('ADMIN_MAIN/goods/addgoodscategory')}">马上添加</a></div>
{/notempty}
{/block}
{block name="script"}
<script type="text/javascript">
$(".table-class input").change(function(){
	var fieldid = $(this).attr('fieldid');
	var fieldname = $(this).attr('fieldname');
	var fieldvalue = $(this).val();
	if(fieldvalue == ''){
		showTip("不能为空哦","warning");
	}else{
		$.ajax({
			type:"post",
			url:"{:__URL('ADMIN_MAIN/goods/modifygoodscategoryfield')}",
			data:{'fieldid':fieldid,'fieldname':fieldname,'fieldvalue':fieldvalue},
			success: function (data) {
				if(data>0){
					showTip("操作成功","success");
				}else{
					showTip("操作失败","error");
				}
			}
		});
	}
});

function tab_switch(module_id){
	if($(".pid_"+module_id).css('display') != 'none'){
		$(".tab_jian_"+module_id).hide();
		$(".tab_jia_"+module_id).show();
		$(".pid_"+module_id).hide(300);
	}else{
		$(".tab_jian_"+module_id).show();
		$(".tab_jia_"+module_id).hide();
		$(".pid_"+module_id).show(500);
	}
}

/* 删除分类 */
function delCategory(category_id) {
	$( "#dialog" ).dialog({
		buttons: {
			"确定": function() {
				$(this).dialog('close');
				$.ajax({
					type : "post",
					url : "{:__URL('ADMIN_MAIN/goods/deletegoodscategory')}",
					data : {
						'category_id' : category_id,
						'goods_category_quick' : JSON.stringify(getCookieGoodsCategory(category_id))
					},
					dataType : "json",
					success : function(data) {
						if(data['code'] > 0){
							showMessage('success', data["message"],"{:__URL('ADMIN_MAIN/goods/goodscategorylist')}");
						}else{
							showMessage('error', data["message"]);
						}
					}
				});
			},
			"取消,#e57373": function() {
				$(this).dialog('close');
			}
		},
		contentText:"你确定删除吗？",
		title:"消息提醒"
	});
}

//查询当前用户最近一天是否发布过商品，修改过商品分类，要删除cookie中对应的值
function getCookieGoodsCategory(category_id){
	var goods_category_quick_new = new Array();// 快速选择商品分类集合
	$.ajax({
		url : "{:__URL('ADMIN_MAIN/goods/getquickgoods')}",
		type : "post",
		async : false,
		success : function(res) {
			var goods_category_quick = eval(res);// 将Cookie中的数据取出来
			for (var i = 0; i < goods_category_quick.length; i++) {
				var quick_name = goods_category_quick[i]["quick_name"];
				var quick_id_arr = goods_category_quick[i]["quick_id"].split(",");
				if(quick_id_arr[quick_id_arr.length-1] == category_id){
					continue;
				}else{
					var json = {
						quick_name : quick_name,
						quick_id : goods_category_quick[i]["quick_id"],
					};
					goods_category_quick_new.push(json);
				}
			}
		}
	});
	return goods_category_quick_new;
}
</script>
{/block}